package com.rocksea.app.fragment;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.rocksea.app.BaseFragment;
import com.rocksea.app.MyApplication;
import com.rocksea.app.R;
import com.rocksea.app.adapter.PileListAdapter;
import com.rocksea.app.db.history.HistoryDbHelper;
import com.rocksea.app.db.history.HistoryTable;
import com.rocksea.app.entry.InspectionForm;
import com.rocksea.app.entry.PileInfo;
import com.rocksea.app.utils.UiUtils;
import com.rocksea.app.weight.DividerListItemDecoration;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.InjectView;

/**
 * @author Administrator
 * @date 2017/11/21
 */

public class SPilNoListFragment extends BaseFragment
{

    @InjectView(R.id.textView5)
    public View m1;
    @InjectView(R.id.textView6)
    public View m2;

    @InjectView(R.id.Pile_List)
    public RecyclerView mRecyclerView;

    @Override
    protected void initView()
    {
        EventBus.getDefault().register(this);
        m1.setVisibility(View.GONE);
        m2.setVisibility(View.GONE);
        setHasOptionsMenu(true);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        // 设置布局管理器
        mRecyclerView.setLayoutManager(layoutManager); //不添加这句 不显示
        mRecyclerView.addItemDecoration(new DividerListItemDecoration(getContext(),DividerListItemDecoration.VERTICAL_LIST));

    }

    @Override
    protected void initData()
    {
        loading();
        queryDataBySerilNo();
    }

    @Override
    protected void requestAgain() {

    }

    @Override
    protected int getContentViewId()
    {
        return R.layout.pile_list_layout;
    }
    public static SPilNoListFragment newInstance(Bundle mBundle)
    {
        SPilNoListFragment sPilNoListFragment =  new SPilNoListFragment();
        sPilNoListFragment.setArguments(mBundle);
        return sPilNoListFragment;
    }

    private void queryDataBySerilNo()
    {
        InspectionForm.InspectionFormListBean mFormListBean = (InspectionForm.InspectionFormListBean) getArguments().get(UiUtils.BUNDLE_KEY);
        if (mFormListBean ==null)
            return;
        String serilNo =  mFormListBean.number;
        int methodCode = UiUtils.judgMentMethod(mFormListBean.applyMethod);
        SAskTask task = new SAskTask(methodCode,getContext(),serilNo);
        task.execute();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onQueryCompete(ArrayList<PileInfo> tables)
    {
        if (tables.size()==0)
        {
            loadFailed();
            mErrorText.setText("暂未检测到数据");
            return;
        }
        loadCompleted();
        PileListAdapter listAdapter = new PileListAdapter(tables, getContext(), PileListAdapter.S);
        mRecyclerView.setAdapter(listAdapter);
    }

    private static class SAskTask extends AsyncTask<Object, Object, ArrayList<PileInfo>>
    {

        private int methodCode;
        private String planNum;

        private SAskTask(int methodCode, Context context,String planNum )
        {
            this.methodCode = methodCode;
            this.planNum = planNum;
        }
        @Override
        protected void onPostExecute(ArrayList<PileInfo> tables)
        {
            super.onPostExecute(tables);
            EventBus.getDefault().post(tables);
        }

        @Override
        protected ArrayList<PileInfo> doInBackground(Object... params)
        {
            ArrayList<HistoryTable> mPileNoData = HistoryDbHelper.getInstance(MyApplication.getContext()).queryBySerilNo(planNum);
            ArrayList<PileInfo> pileInfos = new ArrayList<>();
            for (int i = 0; i < mPileNoData.size(); i++)
            {
                if (mPileNoData.get(i).getType()==methodCode)
                {
                    PileInfo pileInfo = new PileInfo();
                    pileInfo.setPileBottomH("-");
                    pileInfo.setPileId("0");
                    pileInfo.setPileNo(mPileNoData.get(i).getPileNo());
                    pileInfo.setPileTopH("-");
                    pileInfo.setDataType(mPileNoData.get(i).TestMode);
                    pileInfo.setBasicInfoId(mPileNoData.get(i).BaseInfoId);
                    pileInfo.setReceiveTime(mPileNoData.get(i).getTestTime());
                    pileInfo.setSerilNo(planNum);
                    pileInfo.setLoadTime(mPileNoData.get(i).LoadTime);
                    pileInfos.add(pileInfo);
                }
            }
            return pileInfos;
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
    }

    @Override
    public void onDestroy()
    {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
